package com.zhudi.develop_demo_as.ui.activity.pic;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.zhudi.develop.util.ZhudiToastSingle;
import com.zhudi.develop_demo_as.BaseActivity;
import com.zhudi.develop_demo_as.MainApplication;
import com.zhudi.develop_demo_as.R;
import com.zhudi.develop_demo_as.entity.Photo;
import com.zhudi.develop_demo_as.event.OnItemCheckListener;
import com.zhudi.develop_demo_as.ui.fragment.pic.ImagePagerFragment;
import com.zhudi.develop_demo_as.ui.fragment.pic.PhotoPickerFragment;

import java.util.ArrayList;

public class PhotoPickerActivity extends BaseActivity implements View.OnClickListener {
  private Context context;
  private TextView tvFinish;
  private PhotoPickerFragment pickerFragment;
  private ImagePagerFragment imagePagerFragment;
  public final static String EXTRA_MAX_COUNT     = "MAX_COUNT";
  public final static String EXTRA_SHOW_CAMERA   = "SHOW_CAMERA";
  public final static String KEY_SELECTED_PHOTOS = "SELECTED_PHOTOS";

  public final static int DEFAULT_MAX_COUNT = 9;
  private int maxCount = DEFAULT_MAX_COUNT;

  @Override
  protected int getLayoutView() {
    return R.layout.activity_photo_picker;
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = this;
    initView();
    setListener();
  }



  private void initView() {
    ((TextView) findViewById(R.id.tvTitle)).setText("图片");
    visibility(R.id.tvFinish);
    tvFinish = (TextView) findViewById(R.id.tvFinish);

    maxCount = getIntent().getIntExtra(EXTRA_MAX_COUNT, DEFAULT_MAX_COUNT);
    boolean showCamera = getIntent().getBooleanExtra(EXTRA_SHOW_CAMERA, true);

    pickerFragment = (PhotoPickerFragment) getSupportFragmentManager().findFragmentById(R.id.photoPickerFragment);
    pickerFragment.getPhotoGridAdapter().setShowCamera(showCamera);

  }

  private void setListener() {
    tvFinish.setOnClickListener(this);

    pickerFragment.getPhotoGridAdapter().setOnItemCheckListener(new OnItemCheckListener() {
      @Override
      public boolean OnItemCheck(int position, Photo photo, final boolean isCheck, int selectedItemCount) {
        int total = selectedItemCount + (isCheck ? -1 : 1);
//        if (maxCount <= 1) {
//          List<Photo> photos = pickerFragment.getPhotoGridAdapter().getSelectedPhotos();
//          photos.clear();
//          pickerFragment.getPhotoGridAdapter().notifyDataSetChanged();
//          return true;
//        }
        if (total > maxCount) {
          ZhudiToastSingle.showToast(MainApplication.getInstance(), getString(R.string.over_max_count_tips, maxCount), Toast.LENGTH_SHORT, -1);
          return false;
        }
        if (maxCount > 1) {
          String content = getString(R.string.done_with_count);
          tvFinish.setText(String.format(content,total, maxCount));
        }
        return true;
      }
    });
  }

  @Override
  public void onClick(View v) {
    switch (v.getId()){
      case R.id.tvFinish://完成
        ArrayList<String> selectedPhotos = pickerFragment.getPhotoGridAdapter().getSelectedPhotoPaths();
        if (selectedPhotos.size() == 0)
          ZhudiToastSingle.showToast(MainApplication.getInstance(),context.getString(R.string.please_img),Toast.LENGTH_SHORT,-1);
        else {
          Intent intent = new Intent();
          intent.putStringArrayListExtra(KEY_SELECTED_PHOTOS, selectedPhotos);
          setResult(RESULT_OK, intent);
          finish();
        }
        break;
    }
  }


  /**
   * Overriding this method allows us to run our exit animation first, then exiting
   * the activity when it complete.
   */
  @Override
  public void onBackPressed() {
    if (imagePagerFragment != null && imagePagerFragment.isVisible()) {
      imagePagerFragment.runExitAnimation(new Runnable() {
        public void run() {
          if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
            getSupportFragmentManager().popBackStack();
          }
        }
      });
    } else {
      super.onBackPressed();
    }
  }


  public void addImagePagerFragment(ImagePagerFragment imagePagerFragment) {
    this.imagePagerFragment = imagePagerFragment;
    getSupportFragmentManager()
        .beginTransaction()
        .replace(R.id.container, this.imagePagerFragment)
        .addToBackStack(null)
        .commit();
  }


//  @Override public boolean onCreateOptionsMenu(Menu menu) {
//    getMenuInflater().inflate(R.menu.menu_picker, menu);
//    menuDoneItem = menu.findItem(R.id.done);
//    menuDoneItem.setEnabled(false);
//    return true;
//  }
//
//
//  @Override
//  public boolean onOptionsItemSelected(MenuItem item) {
//    if (item.getItemId() == android.R.id.home) {
//      super.onBackPressed();
//      return true;
//    }
//
//    if (item.getItemId() == R.id.done) {
//      Intent intent = new Intent();
//      ArrayList<String> selectedPhotos = pickerFragment.getPhotoGridAdapter().getSelectedPhotoPaths();
//      intent.putStringArrayListExtra(KEY_SELECTED_PHOTOS, selectedPhotos);
//      setResult(RESULT_OK, intent);
//      finish();
//      return true;
//    }
//
//    return super.onOptionsItemSelected(item);
//  }

  public PhotoPickerActivity getActivity() {
    return this;
  }


}
